package com.jr.controller;

import com.jr.pojo.BookingStation;
import com.jr.pojo.Station;
import com.jr.service.IBookingStation;
import com.jr.uitil.PageHelper;
import com.jr.vo.MyBooking;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigInteger;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

@Controller
@RequestMapping("/booking")
public class BookingStationController {

    @Autowired
    private IBookingStation station;


    @RequestMapping("getstation")
    @ResponseBody
    public ArrayList<Station> getStation(String session){
        return station.queryStation(session.substring(0,1));
    }

    @RequestMapping("addstation")
    @ResponseBody
    public boolean addstation(String choosedate, String choosetime, String session, String stid, String profile, String price, Integer userid, BigInteger timestamps){
        System.out.println("过来存储预约了吗");
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
        String format = sdf.format(d);

        Date now = null;

        try {
            now = sdf.parse(format);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Integer stid1=null;
        stid1=Integer.parseInt(stid.substring(stid.length()-2,stid.length()));
        System.out.println("时间戳为"+timestamps);


        BookingStation b = new BookingStation(null,choosedate,choosetime,session.substring(0,1),stid1,profile,Double.parseDouble(price),userid,now,timestamps);
        try {
            boolean b1 = station.addBookingStation(b);
            return b1;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }


    @RequestMapping("checkBooking")
    @ResponseBody
    public ArrayList<MyBooking> checkBooking(Integer pageindex, Integer userid,String choosedate ,Integer status){
        PageHelper ph = new PageHelper();

        System.out.println("过来拿数据了吗");
        System.out.println("userid:"+userid);
        System.out.println("status:"+status);
        MyBooking mb = new MyBooking();
        Date d = new Date();
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
        mb.setUserid(userid);
        mb.setChoosedate(choosedate);
        if(status!=null&&status>=0) {
            mb.setStatus(status);
        }
        ph.setDateCount(station.queryMyBooking(mb,null,null).size());
        //对前端传过来的页码数进行判断
        ph.setPageIndex((pageindex-1)*ph.getShowNumber());
        if(pageindex>ph.getPageCount()){
            ph.setPageIndex(ph.getPageCount());
        }
        if(pageindex<1){
            ph.setPageIndex((pageindex-1)*ph.getShowNumber());
        }

        return station.queryMyBooking(mb,ph.getPageIndex(),ph.getShowNumber());
    }

    //解决多并发时，多人同时访问一个岗位，并且避免该岗位已经被预定仍旧有人去预定
    @RequestMapping("checkDumplicateBook")
    @ResponseBody
    public boolean updatestatus(String choosedate,String choosetime,Integer stid){

        BookingStation b = setBookingStation(choosedate, choosetime, stid);
        BookingStation bookingStation = station.queryDuplicateBooking(b);
        return bookingStation==null;
    }

    @RequestMapping("cancelbooking")
    @ResponseBody
    public boolean cancelBooking(String choosedate,String choosetime,Integer stid){
        System.out.println("过来取消预约了吗");
        System.out.println("stid:"+stid);
        System.out.println("choosetime:"+choosedate);
        BookingStation b = new BookingStation();
        b.setChoosedate(choosedate);
        b.setChoosetime(choosetime);
        b.setStid(stid);
        System.out.println("修改后的日期:"+b.getChoosedate());
        boolean b1 = station.updateMyBookingStatus(b);
        return b1;
    }


    //将转日期格式的硬编码提取出来
    public BookingStation setBookingStation(String choosedate,String choosetime,Integer stid){
        System.out.println("过来判断了吗");
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");

        Date choosedate1=null;
        try {
            choosedate1=sdf1.parse(choosedate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        BookingStation b = new BookingStation();
        b.setChoosetime(choosetime);
        b.setStid(stid);
        return b;
    }

}
